<?php


namespace app\api\controller\User;


use app\api\controller\AppController;
use app\api\model\User\UserFeedbackModel;
use app\api\model\User\UserFeedbackTypeModel;
use app\api\validate\UserAddressValidate;
use app\api\validate\UserFeedbackValidate;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;
use think\exception\ValidateException;

class UserFeedback extends AppController
{

    /**
     * @api {get} /get_feedback_type_list 获取反馈类型
     * @apiGroup 反馈
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiSuccessExample {json} 部分数据
     * {
     *   "id":"类型ID",
     *   "type_name":"类型名称"
     * }
     * @apiVersion 1.0.0
     */
    public function getFeedbackTypeList(Request $request)
    {
        $where = [
            ['is_show','=',1]
        ];
        $list = UserFeedbackTypeModel::getUserFeedbackTypeWhereMore($where,'id,type_name');
        return Util::returnJson(self::CODE_SUCCESS,$list);
    }

    /**
     * @api {get} /get_feedback_list 反馈列表
     * @apiGroup 反馈
     * @apiParam {string} token Token
     * @apiParam {int} page 页码
     * @apiParam {int} limit 数据条数
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiSuccessExample {json} 部分数据
     *  'data':[{
     *   "id":"反馈ID",
     *   "uid":"用户ID",
     *   "type_id":"类型ID",
     *   "content":"内容",
     *   "slider_img":"反馈图片数组",
     * }]
     * @apiVersion 1.0.0
     */
    public function getFeedbackList(Request $request)
    {
        $param = $request->param();
        $user = $this->user;
        if(empty($user)){
            return Util::returnJson(self::CODE_LOGIN,null,"请登录");
        }
        $where = [
            ['uid', '=', $user['id']]
        ];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;

        $list = UserFeedbackModel::getUserFeedbackListWhere($page, $limit, $where);
        $count = UserFeedbackModel::getUserFeedbackCountWhere($where);
        foreach ($list as &$item){
            $images = !empty($item['slider_img']) ? json_decode($item['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $item['slider_img'] = $images;
        }
        return Util::returnPageJson(self::CODE_SUCCESS, $count, $list, "");
    }

    /**
     * @api {get} /get_feedback 反馈详情
     * @apiGroup 反馈
     * @apiParam {string} token Token
     * @apiParam {int} id 反馈ID
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiSuccessExample {json} 部分数据
     * {
     *   "id":"反馈ID",
     *   "uid":"用户ID",
     *   "type_id":"类型ID",
     *   "content":"内容",
     *   "slider_img":"反馈图片数组",
     * }
     * @apiVersion 1.0.0
     */
    public function getFeedback(Request $request)
    {
        $param = $request->param();

        try {
            validate(UserFeedbackValidate::class)->scene('id')->check($param);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return Util::returnJson(self::CODE_ERROR,null,$e->getError());
        }

        $id = !empty($param['id']) ? $param['id'] : 0;
        $feedback = UserFeedbackModel::getUserFeedbackWhere([['id','=',$id]]);
        $images = !empty($feedback['slider_img']) ? json_decode($feedback['slider_img'],true) : [];
        foreach ($images as &$image){
            $image = ImageUtil::i()->replaceUrl($image);
        }
        $feedback['slider_img'] = $images;
        return Util::returnJson(self::CODE_SUCCESS,$feedback);
    }

    /**
     * @api {post} /get_feedback_type_list 添加反馈
     * @apiGroup 反馈
     * @apiParam {string} token Token
     * @apiParam {int} type_id 类型ID
     * @apiParam {string} content 内容
     * @apiParam {file} files 图片数组
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function addFeedback(Request $request)
    {
        $param = $request->param();
        $user = $this->user;
        if(empty($user)){
            return Util::returnJson(self::CODE_LOGIN,null,"请登录");
        }
        try {
            validate(UserFeedbackValidate::class)->scene('info')->check($param);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return Util::returnJson(self::CODE_ERROR,null,$e->getError());
        }

        $type_id = !empty($param['type_id']) ? $param['type_id'] : 0;
        $content = !empty($param['content']) ? $param['content'] : "";
        $feedback = UserFeedbackModel::getUserFeedbackWhere([['uid','=',$user['id']],[['add_time','>',date("Y-m-d 00:00:00",time())]],[['add_time','<',date("Y-m-d 23:59:59",time())]]]);
        if(!empty($feedback)){
            return Util::returnJson(self::CODE_ERROR,null,"一天只能反馈一次");
        }

        $data = [
            'uid'      => $user['id'],
            'type_id'  => $type_id,
            'content'  => $content,
            'up_time'  => date("Y-m-d H:i:s",time()),
            'add_time' => date("Y-m-d H:i:s",time())
        ];

        //多图
        if (!empty($request->file('files'))) {
            $files = $request->file('files');
            $path = "/feedback/uid/".$user['id']."/";
            $urls = ImageUtil::i()->upload($files, $path);
        }

        if (!empty($urls)) {
            $data['slider_img'] = json_encode($urls);
        }

        $id = UserFeedbackModel::addUserFeedback($data);
        if ($id) {
            return Util::returnJson(self::CODE_SUCCESS, $id, "添加成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "添加失败");
        }
    }

}